#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int longestPalindromeSubseq(string s) {
        int n = s.size();
        vector<vector<int>> dp(n, vector<int>(n));

        for (int i = 0; i < n; ++i)
        {
            for (int j = i; j >= 0; --j)
            {
                if (s[i] == s[j])
                    dp[i][j] = j + 1 < i ? dp[i - 1][j + 1] + 2 : i - j + 1;
                else
                    dp[i][j] = max(dp[i - 1][j], dp[i][j + 1]);
            }
        }

        return dp[n - 1][0];
    }
};